<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>JSP复习 | Gip</title>
    <meta name="generator" content="VuePress 1.7.1">
    <link rel="icon" href="/blog/favicon.ico">
    <meta name="description" content="VuePress搭建">
    <meta name="viewport" content="width=device-width,initial-scale=1,user-scalable=no">
    
    <link rel="preload" href="/blog/assets/css/0.styles.41872836.css" as="style"><link rel="preload" href="/blog/assets/js/app.ce46b86f.js" as="script"><link rel="preload" href="/blog/assets/js/2.262e1e49.js" as="script"><link rel="preload" href="/blog/assets/js/8.db580af8.js" as="script"><link rel="prefetch" href="/blog/assets/js/10.8f2ab6cd.js"><link rel="prefetch" href="/blog/assets/js/11.0e5fc533.js"><link rel="prefetch" href="/blog/assets/js/12.61fc7a48.js"><link rel="prefetch" href="/blog/assets/js/13.09133bea.js"><link rel="prefetch" href="/blog/assets/js/14.310e3a44.js"><link rel="prefetch" href="/blog/assets/js/15.d17fc55a.js"><link rel="prefetch" href="/blog/assets/js/16.4550e420.js"><link rel="prefetch" href="/blog/assets/js/17.ceff577e.js"><link rel="prefetch" href="/blog/assets/js/18.b6b9ff56.js"><link rel="prefetch" href="/blog/assets/js/3.115df618.js"><link rel="prefetch" href="/blog/assets/js/4.5b96068b.js"><link rel="prefetch" href="/blog/assets/js/5.7e3d77e5.js"><link rel="prefetch" href="/blog/assets/js/6.0175cad5.js"><link rel="prefetch" href="/blog/assets/js/7.861de91f.js"><link rel="prefetch" href="/blog/assets/js/9.60e428ab.js">
    <link rel="stylesheet" href="/blog/assets/css/0.styles.41872836.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/blog/" class="home-link router-link-active"><img src="/blog/logo.jpg" alt="Gip" class="logo"> <span class="site-name can-hide">Gip</span></a> <div class="links"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><a href="/blog/" class="nav-link">
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Contact" class="dropdown-title"><span class="title">Contact</span> <span class="arrow down"></span></button> <button type="button" aria-label="Contact" class="mobile-dropdown-title"><span class="title">Contact</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/Gip886" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <aside class="sidebar"><nav class="nav-links"><div class="nav-item"><a href="/blog/" class="nav-link">
  Home
</a></div><div class="nav-item"><div class="dropdown-wrapper"><button type="button" aria-label="Contact" class="dropdown-title"><span class="title">Contact</span> <span class="arrow down"></span></button> <button type="button" aria-label="Contact" class="mobile-dropdown-title"><span class="title">Contact</span> <span class="arrow right"></span></button> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="https://github.com/Gip886" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <span><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" focusable="false" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg> <span class="sr-only">(opens new window)</span></span></a></li></ul></div></div> <!----></nav>  <ul class="sidebar-links"><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>vue教程</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group depth-0"><p class="sidebar-heading"><span>Web教程</span> <!----></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blog/views/web/v2.html" class="sidebar-link">本文主要内容</a></li><li><a href="/blog/views/web/v3.html" class="sidebar-link">Web 存储</a></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading open"><span>JSP教程</span> <span class="arrow down"></span></p> <ul class="sidebar-links sidebar-group-items"><li><a href="/blog/views/JSP/01.html" aria-current="page" class="active sidebar-link">JSP复习</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#实验作业复习" class="sidebar-link">实验作业复习</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#实验1" class="sidebar-link">实验1</a></li><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#实验2" class="sidebar-link">实验2</a></li><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#实验3" class="sidebar-link">实验3</a></li><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#实验4" class="sidebar-link">实验4</a></li></ul></li><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#知识详解" class="sidebar-link">知识详解</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#servlet" class="sidebar-link">Servlet</a></li><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#连接数据库操作" class="sidebar-link">连接数据库操作</a></li><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#resultset的常用方法-p162" class="sidebar-link">ResultSet的常用方法：P162</a></li><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#练习-为上述示例添加分页功能-mvc模式移动记录" class="sidebar-link">练习：为上述示例添加分页功能(MVC模式移动记录)</a></li></ul></li><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#不做要求-可以不看" class="sidebar-link">不做要求，可以不看</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#mysql的数据分页方式-limit-start-count-起始位置-总行数" class="sidebar-link">mysql的数据分页方式：limit start,count    ;起始位置，总行数</a></li><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#使用结果集方法更新数据-了解" class="sidebar-link">使用结果集方法更新数据（了解）</a></li><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#_11-预处理语句" class="sidebar-link">11.预处理语句</a></li><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#简易示例-查询-用法与statement一致。" class="sidebar-link">简易示例：查询，用法与Statement一致。</a></li><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#简易示例-带参数添加数据。" class="sidebar-link">简易示例：带参数添加数据。</a></li><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#sql注入示例-登录" class="sidebar-link">SQL注入示例：登录</a></li><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#_12-事务" class="sidebar-link">12. 事务</a></li><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#应用场景示例-模拟转账功能。" class="sidebar-link">应用场景示例：模拟转账功能。</a></li><li class="sidebar-sub-header"><a href="/blog/views/JSP/01.html#数据联动查询" class="sidebar-link">数据联动查询</a></li></ul></li></ul></li></ul></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>软件工程实践</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>算法设计</span> <span class="arrow right"></span></p> <!----></section></li><li><section class="sidebar-group collapsable depth-0"><p class="sidebar-heading"><span>Spring学习</span> <span class="arrow right"></span></p> <!----></section></li></ul> </aside> <main class="page"> <div class="theme-default-content content__default"><h1 id="jsp复习"><a href="#jsp复习" class="header-anchor">#</a> JSP复习</h1> <h2 id="实验作业复习"><a href="#实验作业复习" class="header-anchor">#</a> 实验作业复习</h2> <h3 id="实验1"><a href="#实验1" class="header-anchor">#</a> 实验1</h3> <blockquote><p><strong>实验要点</strong></p> <ol><li><p>指令标记<code>&lt;%= 变量值 %&gt;</code></p></li> <li><p>在页面输出表达式的值：</p> <div class="language-JSP line-numbers-mode"><pre class="language-text"><code>out.print(变量值)
或者
&lt;%=   变量值 %&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><blockquote><p>表达式有：数学表达式/逻辑表达式/比较表达式/函数表达式/一个变量/常量是简单表达式。</p></blockquote> <blockquote><p>注意： =号仅靠%号 不能有空白。</p></blockquote> <blockquote><p>仅仅包含表达式！不能有分号，不能包含java语句。</p></blockquote> <blockquote><p>场景：一般显示变量值，或者对象属性，简化out.print输出</p></blockquote></li> <li><p>全局静态标记<code>&lt;%! %&gt;</code></p> <p>方法的定义并非一定要放在&lt;%!   %&gt;中，只不过放在这里是全局的、静态的；</p> <p>而作为程序片的方法是局部的，或者放在一个类中，在程序片中引用。</p> <p><strong>应用示例1</strong>(for)：创建10行的2*2表格。</p> <div class="language-jsp line-numbers-mode"><pre class="language-text"><code>&lt;%@ page contentType=&quot;text/html;charset=utf-8&quot; %&gt;

&lt;table border=&quot;1&quot;&gt;
&lt;% for(int i=0;i&lt;10;i++) { %&gt;

 &lt;tr&gt;&lt;td&gt; &lt;%= i+1 %&gt;&lt;/td&gt;&lt;/tr&gt;

&lt;%}%&gt;

&lt;/table&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><blockquote><p>要确保结构的完整性！！!</p></blockquote></li></ol></blockquote> <blockquote><ol start="4"><li><p>包含标记<code>&lt;%@ include file=&quot;文件url&quot; %&gt;</code></p> <p>被包含的jsp文件可以放置在JSP页面任意位置。</p> <p>注意：</p> <p>1.&lt;%@ page  %&gt;指令标记的内容必须与主文件100%一致，内容类型\大小写一致。</p> <p>2.被包含的文件如果有中文，必须有&lt;%@ page contentType=&quot;text/html;charset=utf-8&quot; %&gt; 而且与主文件一致，大小写相符，否则中文乱码或者异常500。</p></li></ol></blockquote> <h4 id="实验要求"><a href="#实验要求" class="header-anchor">#</a> 实验要求</h4> <ul><li>1.设计一个网页计数器，每次打开相同的页面时，在页面上显示当前页面被访问的次数</li> <li>2.定义两个方法：1）两数相乘法；2）两数相除；使用Java表达式输出结果。</li> <li>3.在页面中动态输出一个10行的2*2表格</li> <li>注：可以在一个jsp页面中完成上述要求</li></ul> <h4 id="实验步骤"><a href="#实验步骤" class="header-anchor">#</a> 实验步骤</h4> <ol><li>定义静态变量num，在&lt;%! %&gt;标记中可以声明变量和方法</li> <li>使用&lt;% %&gt;标记书写java程序代码，num++</li> <li>使用 &lt;%=  变量值 %&gt;输出变量中的数值或者采用java代码中的out.print(变量值)</li> <li>定义局部变量，并且输出</li> <li>&lt;%! %&gt;中写入2个方法，mult，div功能是乘法和除法</li> <li>在&lt;% %&gt;调用2个方法</li> <li>&lt;%=  变量值 %&gt;输出结果</li> <li>&lt;% %&gt;里面书写for循环，并且结合html标记，输出表格</li></ol> <h4 id="源代码"><a href="#源代码" class="header-anchor">#</a> 源代码</h4> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;
    pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&lt;title&gt;Insert title here&lt;/title&gt;
&lt;/head&gt;
&lt;%! 
    int num; 
	
%&gt;

本页面被访问了 &lt;%= ++num %&gt;次.

&lt;%
    
    int total=0; 
 	total++;
 	out.print(&quot;total当前的值是：&quot;+total);
%&gt;

&lt;%! 
     double mult(double a ,double b){ return a*b;}
     double div(double a ,double b){ return a/b;}
%&gt;
&lt;%
double a=10,b=3;
double r1,r2;

r1=mult(a,b);
r2=div(a,b);
%&gt;
&lt;br&gt;
&lt;%= a %&gt;*&lt;%= b %&gt;=&lt;%=String.format(&quot;%10.2f&quot;,r1) %&gt;&lt;br/&gt;
&lt;%= a %&gt;/&lt;%= b %&gt;=&lt;%= String.format(&quot;%10.2f&quot;,r2) %&gt;

&lt;br&gt;

&lt;table border=&quot;1&quot;&gt;
&lt;% for(int i=0;i&lt;10;i++) { %&gt;

 &lt;tr&gt;&lt;%for(int j =0;j&lt;2;j++){%&gt;
 	&lt;td&gt;&lt;%= i %&gt;&lt;/td&gt;
 	&lt;%} %&gt;
 &lt;/tr&gt;

&lt;%}%&gt;

&lt;/table&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br></div></div><h3 id="实验2"><a href="#实验2" class="header-anchor">#</a> 实验2</h3> <h4 id="jsp动作标记的使用1-include"><a href="#jsp动作标记的使用1-include" class="header-anchor">#</a> JSP动作标记的使用1-include</h4> <p>作用：一般根据条件、在运行时来决定包含哪个页面。[有条件包含(可有参数)，而指令标记include一般用于无条件包含(无参数)]</p> <p>语法：</p> <p>1).不带参数：</p> <p><code>&lt;jsp:include page=&quot;url&quot; /&gt;</code></p> <blockquote><p>不带条件，那么与静态包含效果是一致的</p></blockquote> <p>2).带参数：传递到页面的参数，注意，<strong>动作参数param是自我封闭的标记</strong></p> <p>&lt;jsp:include page=&quot;url&quot;&gt;</p> <p>​	&lt;jsp:param name=&quot;参数名&quot;  value=&quot;参数值&quot;  /&gt;</p> <p>​	&lt;jsp:param name=&quot;参数名&quot;  value=&quot;参数值&quot;  /&gt;</p> <p>​	...</p> <p>​	&lt;jsp:param name=&quot;参数名&quot;  value=&quot;参数值&quot;  /&gt;</p> <p>&lt;/jsp:include&gt;</p> <blockquote><p>request内置对象，获取传递的参数</p> <p><strong>request.getParameter(&quot;参数名或者表单元素name的值&quot;);</strong></p></blockquote> <h4 id="实验要求-2"><a href="#实验要求-2" class="header-anchor">#</a> 实验要求</h4> <ol><li>设计一个页面向被包含页面传递三角形边长参数a,b,c；设计另一个页面，接收参数，实现三角形面积的计算。</li> <li>使用表单输入三角形三边长，在目标页面计算三角形面积。</li></ol> <h4 id="源代码-2"><a href="#源代码-2" class="header-anchor">#</a> 源代码</h4> <p><strong>单页应用</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;%-- 这是index.jsp 主页面 --%&gt;
&lt;%@ page contentType=&quot;text/html;charset=utf-8&quot; %&gt;
&lt;h1&gt;示例：包含目标页面，实现向目标页面传递参数，实现求三角形面积&lt;/h1&gt;

&lt;jsp:include page=&quot;index2.jsp&quot; &gt;
	&lt;jsp:param name=&quot;a&quot; value=&quot;3&quot; /&gt;
	&lt;jsp:param name=&quot;a&quot; value=&quot;4&quot; /&gt;
	&lt;jsp:param name=&quot;a&quot; value=&quot;5&quot; /&gt;
&lt;/jsp:include&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;%--这是index2.jsp 页面 --%&gt;
&lt;%@ page contentType=&quot;text/html;charset=utf-8&quot; %&gt;
&lt;%
   //读取参数值
   String as=request.getParameter(&quot;a&quot;); //所有参数值都是字符串类型
   String bs=request.getParameter(&quot;b&quot;);
   String cs=request.getParameter(&quot;c&quot;);
   
	//注意类型转换；注意补充非空判断
   double a=Double.parseDouble(as);
   double b=Double.parseDouble(bs);
   double c=Double.parseDouble(cs);
   
   //调用函数来实现
   double s=getArea(a,b,c);
   out.print(&quot;&lt;p&gt;三角形的面积是:&quot;+String.format(&quot;%.2f&quot;,s));
%&gt;

&lt;%!
   //定义方法求面积
   double getArea(double a,double b,double c)
   {
      double p=(a+b+c)/2;
      return  Math.sqrt(p*(p-a)*(p-b)*(p-c));
   }
   
%&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><p><strong>使用表单输入三角形三边长，在目标页面计算三角形面积</strong></p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;%-- 这是index.jsp 主页面 --%&gt;
&lt;%@ page contentType=&quot;text/html;charset=utf-8&quot; %&gt;
&lt;h1&gt;示例：包含目标页面，实现向目标页面传递参数，实现求三角形面积&lt;/h1&gt;
&lt;form action=&quot;&quot; method=&quot;post&quot;&gt;
	&lt;input type=&quot;text&quot; name=&quot;a&quot;&gt;&lt;br/&gt;
	&lt;input type=&quot;text&quot; name=&quot;b&quot;&gt;&lt;br/&gt;
	&lt;input type=&quot;text&quot; name=&quot;c&quot;&gt;&lt;br/&gt;
	&lt;input type=&quot;submit&quot; value=&quot;提交&quot;&gt;&lt;br/&gt;
&lt;/form&gt;

&lt;% 
  	String a=request.getParameter(&quot;a&quot;);
	String b=request.getParameter(&quot;b&quot;);
	String c=request.getParameter(&quot;c&quot;);

	if( (a!=null &amp;&amp; !a.equals(&quot;&quot;))  &amp;&amp; 
        (b!=null &amp;&amp; !b.equals(&quot;&quot;)) &amp;&amp;
        (c!=null &amp;&amp; !c.equals(&quot;&quot;)) ) 
    {
%&gt;

&lt;jsp:include page=&quot;index2.jsp&quot; &gt;
	&lt;jsp:param name=&quot;a&quot; value=&quot;&lt;%= a %&gt;&quot; /&gt;
	&lt;jsp:param name=&quot;b&quot; value=&quot;&lt;%= b %&gt;&quot; /&gt;
	&lt;jsp:param name=&quot;c&quot; value=&quot;&lt;%= c %&gt;&quot; /&gt;
&lt;/jsp:include&gt;


&lt;% } else { %&gt;
	&lt;h1&gt;请输入三边长&lt;/h1&gt;
&lt;% } %&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br></div></div><ul><li><p>在直接打开该页面时，abc为null</p></li> <li><p>在没有数据输入就提交时，abc为空字符串</p></li></ul> <h4 id="jsp动作标记2-forward-转发"><a href="#jsp动作标记2-forward-转发" class="header-anchor">#</a> JSP动作标记2-forward(转发)</h4> <p>作用：终止当前页面的执行，转向指定的页面。</p> <p>类似链接功能，但是地址栏的路径不变。</p> <p>语法格式：</p> <p>1)不带参数：  标记必须自我封闭</p> <p><code>&lt;jsp:forward page=&quot;url&quot; /&gt;</code></p> <p>2）带参数：</p> <p>&lt;jsp:forward page=&quot;url&quot; &gt;</p> <p>​	&lt;jsp:param name=&quot;参数名&quot; value=&quot;参数值&quot;  /&gt;</p> <p>​	&lt;jsp:param name=&quot;参数名&quot; value=&quot;参数值&quot;  /&gt;</p> <p>​	...</p> <p>&lt;/jsp:forward&gt;</p> <p>使用场景：登录验证(在登录界面输入帐号，在目标页面验证，如果帐号错误，转向登录界面重新登录；否则转向其他页面/或者提示信息：登录成功)。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;%request.setCharacterEncoding(&quot;utf-8&quot;);%&gt; 
&lt;!-- 避免转向中文乱码 &lt;jsp:param value ...  需要类似网址参数处理 或者转向前采用上面一句--&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h5 id="注意-在属性值中与字符串内容混合在一起的表达式的写法"><a href="#注意-在属性值中与字符串内容混合在一起的表达式的写法" class="header-anchor">#</a> 注意：在属性值中与字符串内容混合在一起的表达式的写法</h5> <p><strong>正确：</strong></p> <div class="language-JSP line-numbers-mode"><pre class="language-text"><code>&lt;jsp:forward page=&quot;formIndex.jsp&quot;&gt;
		&lt;jsp:param name=&quot;ret&quot; value='&lt;%= &quot;这个是输入的数:&quot;+ n %&gt;' &gt;&lt;/jsp:param&gt;
&lt;/jsp:forward&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><p>不正确的显示：</p> <div class="language-JSP line-numbers-mode"><pre class="language-text"><code>&lt;jsp:forward page=&quot;formIndex.jsp&quot;&gt;
		&lt;jsp:param name=&quot;ret&quot; value=&quot;这输入的数:&lt;%= n %&gt;&quot; &gt;&lt;/jsp:param&gt;
&lt;/jsp:forward&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br></div></div><h4 id="补充"><a href="#补充" class="header-anchor">#</a> 补充</h4> <h6 id="resquest对象"><a href="#resquest对象" class="header-anchor">#</a> Resquest对象</h6> <ol><li>设置编码格式<code>request.setCharacterEncoding(&quot;utf-8&quot;);</code></li> <li><code>response.setContentType(&quot;text/html;charset=utf-8&quot;);</code></li> <li>获取表单数据或者其他传递参数方式<code>request.getParameter(&quot;参数名&quot;);</code></li> <li>可以获取URL带参数形式：<code>href=“..........？参数名=参数值”</code></li> <li>获得页面Session方式：<code>HttpSession session=request.getSession();</code></li> <li>Request携带参数方法：<code>request.setAttribute(&quot;参数名&quot;, 参数值或者变量名);</code></li> <li>Session<code>session.setAttribute(&quot;user&quot;, user);</code></li></ol> <h6 id="response对象"><a href="#response对象" class="header-anchor">#</a> response对象</h6> <ol><li><p>重定向方法：<code>response.setHeader(&quot;refresh&quot;,&quot;5;url=new.jsp&quot;);</code></p></li> <li><div class="language- line-numbers-mode"><pre class="language-text"><code>response.setStatus(302);
response.setHeader(&quot;location&quot;,&quot;new.jsp&quot;);
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li> <li><p><code>response.sendRedirect(url)</code><strong>示例</strong>：登录成功、失败重定向到不同页面[测]</p> <p>**提示：**链接a是被动、静态转向</p> <p>&lt;jsp:forward /&gt;一般用于条件转发、可带参数转发</p> <blockquote><p>WEB-INF中的文件只能使用转发（标记forward或者request方法，不能使用链接或重定向）</p></blockquote></li></ol> <h6 id="session对象"><a href="#session对象" class="header-anchor">#</a> Session对象</h6> <p>特点：不同用户session不同，每个用户独有。</p> <p>常用方法：</p> <p>数据保存；读取；移除；清空；列举</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>保存数据：session.setAttribute(&quot;名称&quot;,数据);//数据可以是任意类型

获取数据：(强制转换的类型)session.getAttribute(&quot;名称&quot;);

删除数据：session.removeAttribute(&quot;名称&quot;);

列举数据：Enumeraion&lt;String&gt; en=session.getAttributeNames();

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>应用场景：购物车；登录用户及状态</p> <h6 id="cookie对象"><a href="#cookie对象" class="header-anchor">#</a> Cookie对象</h6> <p>Cookie的使用：创建/保存/读取/删除/修改。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>Cookie ck=new Cookie(&quot;user&quot;,&quot;zs&quot;); //创建
ck.setMaxAge(60*60*24);//24小时  单位：秒

response.addCookie(ck);//写入

Cookies[] cks=request.getCookies();//读取

删除：找到Cookie 设置时间为0，再次添加即可
修改：覆盖，再添加
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br></div></div><p>使用参考：http://www.runoob.com/jsp/jsp-cookies.html</p> <p>示例：登陆时记住密码</p> <h3 id="实验3"><a href="#实验3" class="header-anchor">#</a> 实验3</h3> <h4 id="实验要求-3"><a href="#实验要求-3" class="header-anchor">#</a> 实验要求</h4> <ol><li>在Javabean中进行数据处理：1)设计一个javabean，提供一个代表圆半径的double类型的属性，然后添加取得面积和取得圆周长度的方法；2)设计一个jsp页面，使用该javabean ,输出圆面积和半径</li> <li>使用动作标记，获取和修改Javabean属性：1)设计一个保存学生信息的javabean，保存学生的姓名、年龄和性别；2)设计一个JSP页面，提供一个表单输入学生信息保存到javabean，并使用动作标记读取javabean保存的数据。</li></ol> <h4 id="实验步骤-2"><a href="#实验步骤-2" class="header-anchor">#</a> 实验步骤</h4> <ol><li>创建circle，stduent类，封装好方法</li> <li>创建javabean 给id</li> <li>使用jsp动作标记给对象赋值</li> <li><code>&lt;jsp:setProperty name=&quot;c&quot; property=&quot;radius&quot; param=&quot;change&quot; /&gt;</code></li> <li>输出到页面上面</li></ol> <h4 id="源代码-3"><a href="#源代码-3" class="header-anchor">#</a> 源代码</h4> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;
    pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&lt;title&gt;Insert title here&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;jsp:useBean id=&quot;c&quot; class=&quot;com.gip.Circle&quot; &gt;&lt;/jsp:useBean&gt;
&lt;form&gt;
&lt;input type=&quot;text&quot; name=&quot;change&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;提交&quot;&gt;
&lt;/form&gt;
&lt;/br&gt;

&lt;jsp:setProperty name=&quot;c&quot; property=&quot;radius&quot; param=&quot;change&quot; /&gt;
&lt;%= &quot;圆的半径是:&quot;+c.getRadius() %&gt;
&lt;br/&gt;
&lt;%= &quot;圆的面积是:&quot;+c.circleArea() %&gt;

&lt;/body&gt;
&lt;/html&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br></div></div><div class="language- line-numbers-mode"><pre class="language-text"><code>package com.gip;

public class Circle {
	double radius;
	public Circle(){
		radius=1;
	}
	public double getRadius() {
		return radius;
	}
	public void setRadius(double radius) {
		this.radius = radius;
	}
	public double circleArea(){
		return Math.PI*radius*radius;
	}
	public double circleLength(){
		return 2.0* Math.PI*radius;
	}
}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br></div></div><div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;
    pageEncoding=&quot;UTF-8&quot;%&gt;
&lt;!DOCTYPE html PUBLIC &quot;-//W3C//DTD HTML 4.01 Transitional//EN&quot; &quot;http://www.w3.org/TR/html4/loose.dtd&quot;&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta http-equiv=&quot;Content-Type&quot; content=&quot;text/html; charset=UTF-8&quot;&gt;
&lt;title&gt;Insert title here&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;form&gt;
姓名:&lt;input type=&quot;text&quot; name=&quot;cname&quot;&gt;&lt;br&gt;
年龄:&lt;input type=&quot;text&quot; name=&quot;cage&quot;&gt;&lt;br&gt;
性别:&lt;input type=&quot;text&quot; name=&quot;csex&quot;&gt;
&lt;input type=&quot;submit&quot; value=&quot;提交&quot;&gt;
&lt;/form&gt;
&lt;jsp:useBean id=&quot;s&quot; class=&quot;com.gip.Stdudent&quot; &gt;&lt;/jsp:useBean&gt;
&lt;jsp:setProperty name=&quot;s&quot; property=&quot;name&quot; param=&quot;cname&quot; /&gt;
&lt;jsp:setProperty name=&quot;s&quot; property=&quot;age&quot; param=&quot;cage&quot; /&gt;
&lt;jsp:setProperty name=&quot;s&quot; property=&quot;sex&quot; param=&quot;csex&quot; /&gt;

&lt;%= &quot;学生姓名是:&quot;+s.getName() %&gt;&lt;br&gt;
&lt;%= &quot;学生年龄是:&quot;+s.getAge() %&gt;&lt;br&gt;
&lt;%= &quot;学生性别是:&quot;+s.getSex() %&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br></div></div><h4 id="补充-2"><a href="#补充-2" class="header-anchor">#</a> 补充</h4> <h5 id="javabean"><a href="#javabean" class="header-anchor">#</a> javabean</h5> <p>主要作用：</p> <ol><li>保存数据、处理数据，代码复用；</li></ol> <p>2)视图与代码分离，让jsp仅仅输入数据和从javabean中取得数据来显示。</p> <p>本质:</p> <p>是一个public作用域的简单java类，满足java类的结构和语法要求，但具有一定的约束：</p> <blockquote><p>1)构造方法约束：如果有构造方法，那么构造方法必须是public而且无参数。</p> <p>2)命名约束：所有提供给外部使用的属性，提供public作用域的get/set方法，如假定有属性xxx,则读取方法为getXxx()、赋值方法为setXxx();对于boolean类型数据：可以提供get/set方法；也可以将get方法写为isXxx()格式。</p> <p><strong>3)一个Javabean必须有包名。</strong></p></blockquote> <h6 id="使用动作标记创建"><a href="#使用动作标记创建" class="header-anchor">#</a> 使用动作标记创建</h6> <p><code>&lt;jsp:useBean id=&quot;bean名称&quot; class=&quot;包名.类名&quot; scope=&quot;作用范围&quot; /&gt;</code></p> <blockquote><p>id类似对象名；class包含完整包名的类名；</p></blockquote> <blockquote><p>scope作用范围：page 、request、session和application</p></blockquote> <ol><li><p>page/request: 在jsp页面的创建位置开始存在，直到页面显示完毕，每次打开页面都会重新创建。</p> <ol><li>page每次在当前页面创建，<strong>页面执行完毕销毁</strong>;无论是否转发或重定向到目标页面；</li> <li>request是在当前页面创建，**转发（不是重定向）**到目标页面后，在目标页面不会创建（源和目标都需要使用动作标记scope=request）</li></ol></li> <li><p>区别：如果在当前页面创建了request作用域的对象，那么转发到其他页面，不会重新创建该对象，而page是会创建的</p></li> <li><p><strong>session：类似session中的属性，bean只要创建，在多个页面跳转时就存在，不会重复创建。</strong></p></li> <li><p>application:类似application中的属性，所有用户共享，只要创建就存在，不会重复创建。</p></li></ol> <h6 id="例子index-jsp"><a href="#例子index-jsp" class="header-anchor">#</a> 例子index.jsp</h6> <div class="language-jsp line-numbers-mode"><pre class="language-text"><code> &lt;jsp:useBean id=&quot;myCircle&quot; class=&quot;mybean.Circle&quot; scope=&quot;page&quot; /&gt;

 &lt;%  myCircle.setRadius(10);%&gt;

 &lt;h1&gt;当前半径是：&lt;%= myCircle.getRadius() %&gt;&lt;/h1&gt;
 &lt;h1&gt;当前面积是：&lt;%= myCircle.getArea()   %&gt;&lt;/h1&gt;
 &lt;h1&gt;当前周长是：&lt;%= myCircle.getLength() %&gt;&lt;/h1&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><blockquote><p>在此处与使用一般java类没有什么区别，&lt;jsp:useBean /&gt;标记相当于根据mybean.Circle类，自动创建了名称为myCircle的对象。</p> <p>一般不在java代码中这么使用对象（除非需要调用方法），而是使用动作标记获取或设置属性</p></blockquote> <h6 id="动作标记-获取和修改javabean属性"><a href="#动作标记-获取和修改javabean属性" class="header-anchor">#</a> 动作标记，获取和修改Javabean属性</h6> <blockquote><p>注意：一般不建议使用java代码，使用方法获取属性，而是使用动作标记赋值和取值，原因：JSP页面设计原则：尽量使用标记，减少代码的使用</p></blockquote> <ol><li>getProperty动作标记：取得bean属性</li></ol> <div class="language-jsp line-numbers-mode"><pre class="language-text"><code>&lt;jsp:getProperty name=&quot;bean的id&quot; property=&quot;bean的属性名&quot; /&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><blockquote><p>取代上面使用方法的属性取值</p></blockquote> <p>2）setProperty动作标记：设置属性值</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;jsp:setProperty name=&quot;bean的id&quot; property=&quot;bean的属性名&quot; value=&quot;字符串值&quot; /&gt;
或：
&lt;jsp:setProperty name=&quot;&quot;bean的id&quot; property=&quot;bean的属性名&quot; value=&lt;%= 同类型的属性值 %&gt;

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br></div></div><blockquote><p>方法1中的&quot;字符串值&quot;将被自动转换为对应类型的属性值；</p> <p>方法2中表达式的值必须与bean属性值类型一致(java代码方式,数据类型)。</p> <p>注意：使用动作标记取值或赋值时，必须先创建bean对象。</p></blockquote> <p><strong>3）一般应用示例</strong>：以前面的Student类为例，赋值和取值。</p> <div class="language-JSP line-numbers-mode"><pre class="language-text"><code>&lt;jsp:useBean id=&quot;std&quot; class=&quot;mybean.Student&quot; scope=&quot;request&quot; /&gt;

&lt;jsp:setProperty name=&quot;std&quot;  property=&quot;stdName&quot; value=&quot;张三&quot; /&gt;
&lt;jsp:setProperty name=&quot;std&quot;  property=&quot;age&quot; value=&quot;&lt;%= 28 %&gt;&quot; /&gt; &lt;!--注意双引号！ --&gt;
&lt;jsp:setProperty name=&quot;std&quot;  property=&quot;sex&quot; value=&quot;true&quot; /&gt;

&lt;h1&gt;当前学生信息如下：&lt;/h1&gt;
&lt;p&gt;姓名:&lt;jsp:getProperty name=&quot;std&quot; property=&quot;stdName&quot; /&gt;
&lt;p&gt;年龄:&lt;jsp:getProperty name=&quot;std&quot; property=&quot;age&quot; /&gt;
&lt;p&gt;性别:&lt;jsp:getProperty name=&quot;std&quot; property=&quot;sex&quot; /&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><blockquote><p>注意：value=&quot;值&quot;   ==&gt;值前后无论是字符串，还是表达式，都不能缺少双引号！！！</p> <p>这种在当前页面中赋值和立即取值的场景适合使用scope=page</p></blockquote> <h6 id="_4-使用表单赋值"><a href="#_4-使用表单赋值" class="header-anchor">#</a> <strong>4）使用表单赋值</strong></h6> <p>一般格式：指定参数名</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;jsp:setProperty name=&quot;bean的id&quot; property=&quot;bean的属性名&quot; param=&quot;参数名&quot; /&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>省略参数名：bean的属性名，<strong>必须与表单或链接参数名一一对应</strong>，才会赋值给对应的bean的属性；否则不会赋值。</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;jsp:setProperty name=&quot;bean的id&quot; property=&quot;*&quot;  /&gt;   &lt;!-- 使用 * 号--&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>**应用示例1： 【**当前页面】  ，<u>使用表单</u>为Student赋值</p> <p>testProperty.jsp</p> <div class="language-JSP line-numbers-mode"><pre class="language-text"><code>&lt;jsp:useBean id=&quot;std&quot; class=&quot;mybean.Student&quot; scope=&quot;request&quot; /&gt;

&lt;form action=&quot;&quot; method=&quot;post&quot;&gt;
	&lt;input type=&quot;text&quot; name=&quot;stdName&quot; /&gt;&lt;br/&gt;
	&lt;input type=&quot;text&quot; name=&quot;age&quot; /&gt;&lt;br/&gt;
	&lt;input type=&quot;text&quot; name=&quot;sex&quot; /&gt;&lt;br/&gt;
	&lt;input type=&quot;submit&quot; value=&quot;保存&quot; /&gt;
&lt;/form&gt;

&lt;jsp:setProperty name=&quot;std&quot; property=&quot;*&quot; /&gt;
当前信息：
&lt;p&gt;姓名:&lt;jsp:getProperty name=&quot;std&quot; property=&quot;stdName&quot; /&gt;&lt;/p&gt;
&lt;p&gt;姓名:&lt;jsp:getProperty name=&quot;std&quot; property=&quot;age&quot; /&gt;&lt;/p&gt;
&lt;p&gt;姓名:&lt;jsp:getProperty name=&quot;std&quot; property=&quot;sex&quot; /&gt;&lt;/p&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br></div></div><blockquote><p>这种包含表单提交的场景适和使用scope=request</p></blockquote> <p><strong>应用示例2</strong>：<strong><u>【目标页面】</u></strong>，<u>使用表单</u>为Student赋值</p> <p>myform.jsp</p> <div class="language-JSP line-numbers-mode"><pre class="language-text"><code>&lt;form action=&quot;result.jsp&quot; method=&quot;post&quot;&gt;
	&lt;input type=&quot;text&quot; name=&quot;stdName&quot; /&gt;&lt;br/&gt;
	&lt;input type=&quot;text&quot; name=&quot;age&quot; /&gt;&lt;br/&gt;
	&lt;input type=&quot;text&quot; name=&quot;sex&quot; /&gt;&lt;br/&gt;
	&lt;input type=&quot;submit&quot; value=&quot;保存&quot; /&gt;
&lt;/form&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>result.jsp</p> <div class="language-JSP line-numbers-mode"><pre class="language-text"><code>&lt;jsp:useBean id=&quot;std&quot; class=&quot;mybean.Student&quot; scope=&quot;request&quot; /&gt;
&lt;jsp:setProperty name=&quot;std&quot; property=&quot;*&quot; /&gt;


当前信息：
&lt;p&gt;姓名:&lt;jsp:getProperty name=&quot;std&quot; property=&quot;stdName&quot; /&gt;&lt;/p&gt;
&lt;p&gt;姓名:&lt;jsp:getProperty name=&quot;std&quot; property=&quot;age&quot; /&gt;&lt;/p&gt;
&lt;p&gt;姓名:&lt;jsp:getProperty name=&quot;std&quot; property=&quot;sex&quot; /&gt;&lt;/p&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><blockquote><p>来源页面未创建过javabean ,result.jsp中将创建</p></blockquote> <p><strong>应用示例3：<u>使用链接</u>为Student赋值,表单元素值为字符串</strong></p> <p>testProperty.jsp</p> <div class="language-JSP line-numbers-mode"><pre class="language-text"><code>&lt;jsp:useBean id=&quot;std&quot; class=&quot;mybean.Student&quot; scope=&quot;request&quot; /&gt;
&lt;jsp:setProperty name=&quot;std&quot; property=&quot;*&quot; /&gt;
&lt;a href=&quot;testProperty.jsp?stdName=zs&amp;age=19&amp;sex=true&quot; &gt;单击我试试&lt;/a&gt;

当前信息：
&lt;p&gt;姓名:&lt;jsp:getProperty name=&quot;std&quot; property=&quot;stdName&quot; /&gt;&lt;/p&gt;
&lt;p&gt;姓名:&lt;jsp:getProperty name=&quot;std&quot; property=&quot;age&quot; /&gt;&lt;/p&gt;
&lt;p&gt;姓名:&lt;jsp:getProperty name=&quot;std&quot; property=&quot;sex&quot; /&gt;&lt;/p&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><blockquote><p>使用page与request均可:都会每次在打开页面时创建</p></blockquote> <h3 id="实验4"><a href="#实验4" class="header-anchor">#</a> 实验4</h3> <h4 id="实验要求-4"><a href="#实验要求-4" class="header-anchor">#</a> 实验要求</h4> <ol><li>使用response对象，实现输出任意类型文档：附件方式打开</li> <li>制作登录页面，模拟登录，当输入帐号正确则转向成功页面；否则转向失败页面</li></ol> <h4 id="源代码-4"><a href="#源代码-4" class="header-anchor">#</a> 源代码</h4> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;%@page import=&quot;java.io.*&quot;%&gt;
&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;
    pageEncoding=&quot;UTF-8&quot; %&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;Insert title here&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;%
response.setContentType(&quot;application/octet-stream&quot;);


response.setHeader(&quot;content-disposition&quot;, &quot;attachment;filename=test.jpg&quot;);

String filename=&quot;img/1.jpg&quot;;
filename=application.getRealPath(filename);
System.out.println(filename);
InputStream in=new FileInputStream(filename);
byte[] buf=new byte[1024];
int len=in.read(buf,0,buf.length);

OutputStream os=response.getOutputStream();
while(len&gt;0){
	os.write(buf, 0, len);
	len=in.read(buf,0,buf.length);
}

os.close();
in.close();
 
out.clear();
out=pageContext.pushBody();
%&gt;
&lt;/body&gt;
&lt;/html&gt;

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br></div></div><div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;%@page import=&quot;java.io.*&quot;%&gt;
&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;
    pageEncoding=&quot;UTF-8&quot; %&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;Insert title here&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;%
response.setContentType(&quot;application/octet-stream&quot;);


response.setHeader(&quot;content-disposition&quot;, &quot;attachment;filename=test.jpg&quot;);

String filename=&quot;img/1.jpg&quot;;
filename=application.getRealPath(filename);
System.out.println(filename);
InputStream in=new FileInputStream(filename);
byte[] buf=new byte[1024];
int len=in.read(buf,0,buf.length);

OutputStream os=response.getOutputStream();
while(len&gt;0){
	os.write(buf, 0, len);
	len=in.read(buf,0,buf.length);
}

os.close();
in.close();
 
out.clear();
out=pageContext.pushBody();
%&gt;
&lt;/body&gt;
&lt;/html&gt;

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br></div></div><h2 id="知识详解"><a href="#知识详解" class="header-anchor">#</a> 知识详解</h2> <h3 id="servlet"><a href="#servlet" class="header-anchor">#</a> Servlet</h3> <h4 id="常会用方法"><a href="#常会用方法" class="header-anchor">#</a> 常会用方法</h4> <ol><li><p><code>response.setContentType(&quot;text/html;charset=utf-8&quot;);</code> <code>PrintWriter out=response.getWriter();</code></p></li> <li><div class="language- line-numbers-mode"><pre class="language-text"><code>requestDispatcher disp=request.getRequstDispatcher(&quot;test.jsp&quot;);
            disp.forward(request,response); 
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div></li> <li><p>在Servlet中获取session对象：<code>HttpSession session=request.getSession();</code></p></li></ol> <h3 id="连接数据库操作"><a href="#连接数据库操作" class="header-anchor">#</a> 连接数据库操作</h3> <h4 id="常会用方法-2"><a href="#常会用方法-2" class="header-anchor">#</a> 常会用方法</h4> <div class="language- line-numbers-mode"><pre class="language-text"><code>String url=&quot;jdbc:mysql://localhost:3306/gip?&quot;;
url+=&quot;user=root&amp;password=123456&amp;characterEncoding=utf-8&amp;serverTimezone=UTC&quot;;
Connection con=DriverManager.getConnection(url);
		Statement st=con.createStatement();
		String sql=&quot;select * from user&quot;;
		ResultSet rs=st.executeQuery(sql);
		while(rs.next()) {
		//处理内容
			String logname=rs.getString(&quot;logname&quot;);//字段名或者列号
			String password=rs.getString(&quot;password&quot;);
			String phone=rs.getString(&quot;phone&quot;);
			String addess=rs.getString(&quot;addess&quot;);
			String realname=rs.getString(&quot;realname&quot;);
			//尽量写字段名
		}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br></div></div><h5 id="预处理方式"><a href="#预处理方式" class="header-anchor">#</a> 预处理方式</h5> <div class="language- line-numbers-mode"><pre class="language-text"><code>Connection con=DriverManager.getConnection(url);
			String sql=&quot;insert into shoppingform value(?,?,?)&quot;;
			PreparedStatement pt=con.prepareStatement(sql);
			pt.setString(1, user.getLogname());
			pt.setString(2, mobile_name);
			pt.setFloat(3,price);
			int i=pt.executeUpdate();
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br></div></div><p><code>String sql=&quot;INSERT INTO usertable (user_name,user_password,user_sex,province_id,city_id,user_describe) VALUES (?,?,?,?,?,?)&quot;;</code></p> <h3 id="resultset的常用方法-p162"><a href="#resultset的常用方法-p162" class="header-anchor">#</a> ResultSet的常用方法：P162</h3> <p><strong>前提使用：可以前后移动数据。</strong></p> <p>next()：下一行</p> <p>first() / last() :第1行/最后一行</p> <p>absolute(n) ：移动到指定的行号  ，<strong>行号从1开始</strong> 。</p> <p>getRow() : 取得当前的行号 。<strong>行号从1开始</strong></p> <p>beforeFirst() :移动第1行之前(超出行首，无数据,不能读取数据)</p> <p>afterLast():移到最后一行之后（超出行尾，无数据，不能读取数据）</p> <p>判断：</p> <p>isFirst()/isLast() :是否第1行/是否最后一行</p> <h4 id="数据分页方法"><a href="#数据分页方法" class="header-anchor">#</a> 数据分页方法</h4> <p>1.取得表数据行数(记录数)：rows  =&gt;rs.last()；=&gt;rs.getRow();// 移动到最后一行，取得行号=总行数</p> <p>2.每页记录数：pageSize；//可设定</p> <p>3.总页数：pageCount   =&gt;  pageCount=   Math.ceil( rows*1.0/pageSize);</p> <p>​		比如表格有33 行，每次页面显示10行数据，那么将分为【33*1.0/10=&gt;3.3=&gt;4页】</p> <p>4.当前页：pageIndex</p> <p>5.当前页起始行：rowStart    ;   rowStart= pageIndex*pageSize +1； //行数从1开始</p> <p>6.当前页结束行：rowEnd   ；   rowEnd=rowStart+pageSize .</p> <p>7.当前页的行数据：每次读取固定行，不够行数则退出继续读取</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>boolean bol=rs.absolute(rowStart-1);  
if(bol)
{
	for(int i=rowStart;i&lt;rowEnd;i++)
	{

		if(!rs.next())  break; //最后一页可能不够pageSize行
		//读取数据,直接输出行数据； 或者，保存到javabean数组,然后转发到jsp页面去显示
	}
}

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br></div></div><p>//关闭操作</p> <p>index.jsp分页实现</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;
    pageEncoding=&quot;UTF-8&quot; import=&quot;java.sql.*,java.util.*&quot;%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;Insert title here&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;%
	int pageSize=3;
	int pageIndex=0;
	int rows;//=10;
	int pageCount;//=(int)Math.ceil(rows*1.0/pageSize);


	String s=request.getParameter(&quot;pageIndex&quot;);
	if(s!=null &amp;&amp; !s.equals(&quot;&quot;))
	{
		pageIndex=Integer.parseInt(s);		 
	}
	
	String url=&quot;jdbc:mysql://localhost:3306/warehourse?user=root&amp;password=123&amp;characterEncoding=utf-8&amp;serverTimezone=UTC&quot;;
	Connection conn=DriverManager.getConnection(url);
	Statement st=conn.createStatement();
	ResultSet rs=st.executeQuery(&quot;select * from product&quot;);
	
	rs.last();
	rows=rs.getRow();
	
	pageCount=(int)Math.ceil(rows*1.0/pageSize);	
	if(pageIndex&gt;=pageCount) pageIndex=pageCount-1;
	
	int startRow=pageIndex*pageSize+1;
		
	for(int i=startRow;i&lt;=startRow+pageSize;i++)
	{
		if(rs.absolute(i))
		{
			out.print(rs.getString(&quot;pid&quot;)+&quot;,&quot;+rs.getString(&quot;name&quot;)+&quot;&lt;br&gt;&quot;);
		}
	}
	
	
	
	rs.close();
	st.close();
	conn.close();
%&gt;
&lt;a href=&quot;index.jsp?pageIndex=&lt;%= pageIndex+1 %&gt;&quot;&gt;下一页&lt;/a&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br></div></div><h3 id="练习-为上述示例添加分页功能-mvc模式移动记录"><a href="#练习-为上述示例添加分页功能-mvc模式移动记录" class="header-anchor">#</a> 练习：为上述示例添加分页功能(MVC模式移动记录)</h3> <p>分页器类Pager.java</p> <div class="language-JAVA line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Pager</span> <span class="token punctuation">{</span>
	  <span class="token keyword">int</span> totalPage<span class="token punctuation">;</span>
	  <span class="token keyword">int</span> curPage<span class="token punctuation">;</span>
	  <span class="token keyword">int</span> pageSize<span class="token punctuation">;</span>
	  <span class="token comment">//...省略了getter/setter	  </span>
	  <span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><p>javabean类：Prov.java</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">Prov</span> <span class="token punctuation">{</span>
	  <span class="token keyword">int</span> province_id<span class="token punctuation">;</span> 
	  <span class="token class-name">String</span> province_code<span class="token punctuation">;</span> 
	  <span class="token class-name">String</span> province_name<span class="token punctuation">;</span>
    <span class="token comment">//省略了getter/setter</span>
	  <span class="token punctuation">}</span>
	  
	
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><p>servlet类：ProvinceServletPage.java</p> <div class="language-JAVA line-numbers-mode"><pre class="language-java"><code><span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">doGet</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
		<span class="token comment">// TODO Auto-generated method stub</span>
		<span class="token comment">//response.getWriter().append(&quot;Served at: &quot;).append(request.getContextPath());</span>
		request<span class="token punctuation">.</span><span class="token function">setCharacterEncoding</span><span class="token punctuation">(</span><span class="token string">&quot;utf-8&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">String</span> provName<span class="token operator">=</span><span class="token string">&quot;&quot;</span><span class="token punctuation">;</span><span class="token comment">//request.getParameter(&quot;provName&quot;);</span>
		
		<span class="token keyword">try</span> <span class="token punctuation">{</span>
			<span class="token class-name">String</span> connstr<span class="token operator">=</span><span class="token string">&quot;jdbc:mysql://localhost:3306/del?user=yan1234&amp;password=1234&amp;characterEncoding=utf-8&amp;serverTimezone=Asia/Shanghai&quot;</span><span class="token punctuation">;</span>
			<span class="token class-name">Connection</span> conn<span class="token operator">=</span><span class="token class-name">DriverManager</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span>connstr<span class="token punctuation">)</span><span class="token punctuation">;</span>
			
			<span class="token class-name">String</span> sql<span class="token operator">=</span><span class="token string">&quot;select * from bs_province where province_name like '%&quot;</span><span class="token operator">+</span>provName<span class="token operator">+</span><span class="token string">&quot;%'&quot;</span><span class="token punctuation">;</span>
			<span class="token class-name">Statement</span> st<span class="token operator">=</span>conn<span class="token punctuation">.</span><span class="token function">createStatement</span><span class="token punctuation">(</span> <span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token class-name">ResultSet</span> rs<span class="token operator">=</span> st<span class="token punctuation">.</span><span class="token function">executeQuery</span><span class="token punctuation">(</span>sql<span class="token punctuation">)</span><span class="token punctuation">;</span>
			
			rs<span class="token punctuation">.</span><span class="token function">last</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">int</span> pageSize<span class="token operator">=</span><span class="token number">2</span><span class="token punctuation">;</span>
			<span class="token keyword">int</span> totalPage<span class="token operator">=</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span> <span class="token class-name">Math</span><span class="token punctuation">.</span><span class="token function">ceil</span><span class="token punctuation">(</span>rs<span class="token punctuation">.</span><span class="token function">getRow</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">*</span><span class="token number">1.0</span><span class="token operator">/</span>pageSize<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token class-name">String</span> c<span class="token operator">=</span>request<span class="token punctuation">.</span><span class="token function">getParameter</span><span class="token punctuation">(</span><span class="token string">&quot;curPage&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">int</span> curPage<span class="token operator">=</span>c<span class="token operator">==</span><span class="token keyword">null</span><span class="token operator">?</span><span class="token number">0</span><span class="token operator">:</span><span class="token class-name">Integer</span><span class="token punctuation">.</span><span class="token function">parseInt</span><span class="token punctuation">(</span>c<span class="token punctuation">)</span><span class="token punctuation">;</span>
			
			<span class="token class-name">Pager</span> pager<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Pager</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			pager<span class="token punctuation">.</span><span class="token function">setCurPage</span><span class="token punctuation">(</span>curPage<span class="token punctuation">)</span><span class="token punctuation">;</span>
			pager<span class="token punctuation">.</span><span class="token function">setPageSize</span><span class="token punctuation">(</span>pageSize<span class="token punctuation">)</span><span class="token punctuation">;</span>
			pager<span class="token punctuation">.</span><span class="token function">setTotalPage</span><span class="token punctuation">(</span>totalPage<span class="token punctuation">)</span><span class="token punctuation">;</span>
			
			
			rs<span class="token punctuation">.</span><span class="token function">absolute</span><span class="token punctuation">(</span>pageSize<span class="token operator">*</span>curPage<span class="token operator">+</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>			
			<span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Prov</span><span class="token punctuation">&gt;</span></span> provList<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">Prov</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>	
			
			<span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i<span class="token operator">=</span><span class="token number">0</span><span class="token punctuation">;</span>i<span class="token operator">&lt;</span>pageSize<span class="token punctuation">;</span>i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
				 <span class="token class-name">Prov</span> prov<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Prov</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				 prov<span class="token punctuation">.</span>province_id<span class="token operator">=</span>rs<span class="token punctuation">.</span><span class="token function">getInt</span><span class="token punctuation">(</span><span class="token string">&quot;province_id&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				 prov<span class="token punctuation">.</span>province_code<span class="token operator">=</span>rs<span class="token punctuation">.</span><span class="token function">getString</span><span class="token punctuation">(</span><span class="token string">&quot;province_code&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				 prov<span class="token punctuation">.</span>province_name<span class="token operator">=</span>rs<span class="token punctuation">.</span><span class="token function">getString</span><span class="token punctuation">(</span><span class="token string">&quot;province_name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				 
				 provList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>prov<span class="token punctuation">)</span><span class="token punctuation">;</span>
				 rs<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>				 
			<span class="token punctuation">}</span>
			 
			 
			rs<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			st<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			conn<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			
			request<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;provList&quot;</span><span class="token punctuation">,</span> provList<span class="token punctuation">)</span><span class="token punctuation">;</span>
			request<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;pager&quot;</span><span class="token punctuation">,</span> pager<span class="token punctuation">)</span><span class="token punctuation">;</span>
			request<span class="token punctuation">.</span><span class="token function">getRequestDispatcher</span><span class="token punctuation">(</span><span class="token string">&quot;resultPage.jsp&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forward</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> response<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span><span class="token keyword">catch</span><span class="token punctuation">(</span><span class="token class-name">SQLException</span> ex<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">println</span><span class="token punctuation">(</span><span class="token string">&quot;??&quot;</span><span class="token operator">+</span>ex<span class="token punctuation">.</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br></div></div><p>JSP:resultPage.jsp</p> <div class="language-JSP line-numbers-mode"><pre class="language-text"><code>&lt;body&gt;
	&lt;form action=&quot;ProvinceServletPage&quot; method=&quot;post&quot;&gt;
		&lt;input name=&quot;provName&quot;&gt;
		&lt;button&gt;查询&lt;/button&gt;
	&lt;/form&gt;

	&lt;%
		List&lt;Prov&gt; provList = (List&lt;Prov&gt;) request.getAttribute(&quot;provList&quot;);
		Pager pager=(Pager)request.getAttribute(&quot;pager&quot;);
	%&gt;
	&lt;table border=&quot;1&quot;&gt;
		&lt;tr&gt;
			&lt;th&gt;ID&lt;/th&gt;
			&lt;th&gt;代码&lt;/th&gt;
			&lt;th&gt;名称&lt;/th&gt;
			&lt;th&gt;建立时间&lt;/th&gt;
		&lt;/tr&gt;

		&lt;%
			for (Prov p : provList) {
		%&gt;
		&lt;tr&gt;
			&lt;td&gt;&lt;%=p.getProvince_id()%&gt;&lt;/td&gt;
			&lt;td&gt;&lt;%=p.getProvince_code()%&gt;&lt;/td&gt;
			&lt;td&gt;&lt;%=p.getProvince_name()%&gt;&lt;/td&gt;
			&lt;td&gt;&lt;%=p.getGmt_create()%&gt;&lt;/td&gt;
		&lt;/tr&gt;

		&lt;%
			}
		%&gt;

	&lt;/table&gt;
	
	&lt;br/&gt;
	&lt;a href=&quot;ProvinceServletPage?curPage=&lt;%= pager.getCurPage()+1 %&gt;&quot; &gt;下一页&lt;/a&gt;
&lt;/body&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br></div></div><h2 id="不做要求-可以不看"><a href="#不做要求-可以不看" class="header-anchor">#</a> 不做要求，可以不看</h2> <h3 id="mysql的数据分页方式-limit-start-count-起始位置-总行数"><a href="#mysql的数据分页方式-limit-start-count-起始位置-总行数" class="header-anchor">#</a> mysql的数据分页方式：limit start,count    ;起始位置，总行数</h3> <div class="language-JSP line-numbers-mode"><pre class="language-text"><code>protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		//response.getWriter().append(&quot;Served at: &quot;).append(request.getContextPath());
		response.setContentType(&quot;text/html;charset=utf-8&quot;);
		PrintWriter out=response.getWriter();//.append(&quot;Served at: &quot;).append(request.getContextPath());
		
		
		String connstr=&quot;jdbc:mysql://localhost:3306/book?user=yan1234&amp;password=1234&amp;characterEncoding=utf-8&amp;serverTimezone=Asia/Shanghai&quot;;
		try {
			
			
			Connection conn =DriverManager.getConnection(connstr);
			Statement st=conn.createStatement();
			
			int pageSize=2;			
			int curPage=request.getParameter(&quot;curPage&quot;)==null?0:Integer.parseInt(request.getParameter(&quot;curPage&quot;));
			if(curPage&lt;0) curPage=0;
			int curRow=curPage*pageSize;
			
			ResultSet rs= st.executeQuery(&quot;select * from booklist limit &quot;+curRow+&quot;,&quot;+pageSize);
			
			
			int count=0;
			while(rs.next()) {
				out.print(rs.getRow()+&quot;.&quot;+rs.getString(&quot;bookName&quot;)+&quot;&lt;br/&gt;&quot;);
				count++;
			} 
			
			rs.close();
			st.close();
			conn.close();
			
			//判断是否已经读取完毕，否则在下一页链接中不断移动读取数据
			if(count&lt;pageSize || count==0) {
				out.print(&quot;读取完毕！&quot;);
				curPage=curPage-1;
				response.sendRedirect(&quot;mylimit?curPage=&quot;+curPage);
				//使用limit会不断移动数据，即使数据读取完毕；这里做个判断
			}
			 
			
			out.print(&quot;&lt;a href=mylimit?curPage=&quot; +(curPage-1)+ &quot; &gt;上一行|&lt;/a&gt;&quot;);	
			out.print(&quot;&lt;a href=mylimit?curPage=&quot; +(curPage+1)+ &quot; &gt;下一行&lt;/a&gt;&quot;);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br></div></div><h3 id="使用结果集方法更新数据-了解"><a href="#使用结果集方法更新数据-了解" class="header-anchor">#</a> 使用结果集方法更新数据（了解）</h3> <p>简单示例</p> <p>Statement（敏感/不敏感,可以修改的） //不能默认，只能设置为可修改</p> <h4 id="_1-更新"><a href="#_1-更新" class="header-anchor">#</a> 1.更新</h4> <p>rs.absolute(n);//移动到需要修改的记录</p> <p>rs.update字段类型(&quot;字段名&quot;,字段值); //赋新值</p> <p>rs.updateRow();//执行更新</p> <h4 id="_2-添加数据"><a href="#_2-添加数据" class="header-anchor">#</a> 2.添加数据</h4> <p>实际是插入新的空行，然后赋值，最后执行添加操作。</p> <p>rs.moveToInsertRow();</p> <p>rs.update字段类型(&quot;字段名&quot;,字段值);//赋值</p> <p>rs.insertRow();//执行数据插入</p> <blockquote><p>字符串日期转换到数据库的方法：</p> <p>String strDate=&quot;2019-10-12&quot;;</p> <p>SimpleDateFormat sdf=new SimpleDateFormat(&quot;yyyy-MM-dd&quot;);</p> <p><strong>java.util</strong>.Date date=sdf.parse(strDate);//这是java.util.Date类型 ，默认在servlet中是java.sql.Date类型，该类型使用长整数日期保存到数据库</p> <p><strong>java.sql</strong>.Data dbDate= new  java.sql.Data (    date.getTime()   );</p></blockquote> <p>updateDate(&quot;日期字段名&quot;,sql.Date类型数据)；</p> <h4 id="_3-删除数据"><a href="#_3-删除数据" class="header-anchor">#</a> 3.删除数据</h4> <p>移动到要删除的行，执行删除行操作。</p> <p>rs.absolute(1);</p> <p>rs.deleteRow()</p> <hr> <h3 id="_11-预处理语句"><a href="#_11-预处理语句" class="header-anchor">#</a> 11.预处理语句</h3> <p>优点：</p> <p>1.提高性能：无须每次编译SQL语句。</p> <p>2.可以使用命令参数，不需要拼接SQL语句。</p> <p>3.安全：防止SQL注入。Statement如登录 ，用户名：随便；密码 ：  ' or  '1=1  例如</p> <div class="language- line-numbers-mode"><pre class="language-text"><code>&quot;select * from user where   username='&quot;+u+&quot;' and pwd='&quot;+p+&quot;'&quot;  ;//注入成功
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>示例1：使用Statement插入省份数据,并获取新记录的关键字（自动递增的ID)</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">doGet</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
 <span class="token class-name">String</span> connstring<span class="token operator">=</span><span class="token string">&quot;jdbc:mysql://localhost:3306/del?user=yan1234&amp;password=1234&amp;characterEncoding=utf-8&amp;serverTimezone=Asia/Shanghai&quot;</span><span class="token punctuation">;</span>
    
		<span class="token class-name">String</span> code<span class="token operator">=</span><span class="token string">&quot;1112&quot;</span><span class="token punctuation">;</span>
		<span class="token class-name">String</span> name<span class="token operator">=</span><span class="token string">&quot;232&quot;</span><span class="token punctuation">;</span>
		<span class="token class-name">String</span> gmt<span class="token operator">=</span><span class="token string">&quot;2019-10-10 23:23:12&quot;</span><span class="token punctuation">;</span>
		<span class="token class-name">String</span> sname<span class="token operator">=</span><span class="token string">&quot;22&quot;</span><span class="token punctuation">;</span>
		<span class="token keyword">try</span> <span class="token punctuation">{</span>
			<span class="token class-name">Connection</span> conn<span class="token operator">=</span>conn<span class="token operator">=</span><span class="token class-name">DriverManager</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span>connstring<span class="token punctuation">)</span><span class="token punctuation">;</span> 
			<span class="token class-name">Statement</span> st<span class="token operator">=</span>conn<span class="token punctuation">.</span><span class="token function">createStatement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token class-name">String</span> sql<span class="token operator">=</span><span class="token string">&quot;insert into bs_province(province_code,province_name,gmt_create,short_name) &quot;</span><span class="token punctuation">;</span>
			sql<span class="token operator">+=</span><span class="token string">&quot; values('&quot;</span><span class="token operator">+</span>code<span class="token operator">+</span><span class="token string">&quot;','&quot;</span><span class="token operator">+</span>name<span class="token operator">+</span><span class="token string">&quot;','&quot;</span><span class="token operator">+</span>gmt<span class="token operator">+</span><span class="token string">&quot;','&quot;</span><span class="token operator">+</span>sname<span class="token operator">+</span><span class="token string">&quot;')&quot;</span><span class="token punctuation">;</span>
			st<span class="token punctuation">.</span><span class="token function">executeUpdate</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> <span class="token class-name">Statement</span><span class="token punctuation">.</span>RETURN_GENERATED_KEYS<span class="token punctuation">)</span><span class="token punctuation">;</span>
			
			<span class="token class-name">ResultSet</span> rs<span class="token operator">=</span>st<span class="token punctuation">.</span><span class="token function">getGeneratedKeys</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			rs<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">int</span> id<span class="token operator">=</span>rs<span class="token punctuation">.</span><span class="token function">getInt</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;id=&quot;</span><span class="token operator">+</span>id<span class="token punctuation">)</span><span class="token punctuation">;</span>
			
			rs<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			st<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			conn<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			
		<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">SQLException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token comment">// TODO Auto-generated catch block</span>
			e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
		
	<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br></div></div><p>示例2：使用PreparedStatement</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">doGet</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
<span class="token class-name">String</span> connstring<span class="token operator">=</span><span class="token string">&quot;jdbc:mysql://localhost:3306/del?user=yan1234&amp;password=1234&amp;characterEncoding=utf-8&amp;serverTimezone=Asia/Shanghai&quot;</span><span class="token punctuation">;</span>
    
		<span class="token class-name">String</span> sql<span class="token operator">=</span><span class="token string">&quot;insert into bs_province(province_code,province_name,gmt_create,gmt_modified,SHORT_NAME)&quot;</span><span class="token punctuation">;</span>
		sql<span class="token operator">+=</span><span class="token string">&quot; values(?,?,?,?,?)&quot;</span> <span class="token punctuation">;</span>
		
		
		<span class="token keyword">try</span> <span class="token punctuation">{</span>
            <span class="token class-name">Connection</span> conn<span class="token operator">=</span>conn<span class="token operator">=</span><span class="token class-name">DriverManager</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span>connstring<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token class-name">PreparedStatement</span> pst<span class="token operator">=</span>conn<span class="token punctuation">.</span><span class="token function">prepareStatement</span><span class="token punctuation">(</span>sql<span class="token punctuation">,</span> <span class="token class-name">Statement</span><span class="token punctuation">.</span>RETURN_GENERATED_KEYS<span class="token punctuation">)</span><span class="token punctuation">;</span>
			pst<span class="token punctuation">.</span><span class="token function">setString</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">&quot;111&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			pst<span class="token punctuation">.</span><span class="token function">setString</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span> <span class="token string">&quot;测试省份&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token comment">//</span>
			<span class="token class-name">String</span> dateStr<span class="token operator">=</span><span class="token class-name">LocalDate</span><span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">&quot; &quot;</span><span class="token operator">+</span><span class="token class-name">LocalTime</span><span class="token punctuation">.</span><span class="token function">now</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		    pst<span class="token punctuation">.</span><span class="token function">setString</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> dateStr<span class="token punctuation">)</span><span class="token punctuation">;</span>
		    pst<span class="token punctuation">.</span><span class="token function">setString</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> dateStr<span class="token punctuation">)</span><span class="token punctuation">;</span>		    
		    pst<span class="token punctuation">.</span><span class="token function">setString</span><span class="token punctuation">(</span><span class="token number">5</span><span class="token punctuation">,</span> <span class="token string">&quot;测试&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		    pst<span class="token punctuation">.</span><span class="token function">executeUpdate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span> 

		    pst<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		    conn<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span>
		    
		    <span class="token class-name">System</span><span class="token punctuation">.</span>out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;保存成功！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			
		<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">SQLException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token comment">// TODO Auto-generated catch block</span>
			e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
 
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br></div></div><h3 id="简易示例-查询-用法与statement一致。"><a href="#简易示例-查询-用法与statement一致。" class="header-anchor">#</a> 简易示例：查询，用法与Statement一致。</h3> <div class="language- line-numbers-mode"><pre class="language-text"><code>&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;
    pageEncoding=&quot;UTF-8&quot;%&gt;
    
&lt;%@ page import=&quot;java.sql.*&quot; %&gt;   
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;Insert title here&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;%
	Class.forName(&quot;com.mysql.cj.jdbc.Driver&quot;);
	String url=&quot;jdbc:mysql://192.168.100.13:3306/warehourse?user=root&amp;password=123&amp;characterEncoding=utf-8&amp;serverTimezone=UTC&quot;;
	
	Connection conn=DriverManager.getConnection(url);
	
	/* Statement st= conn.createStatement(); 
	ResultSet rs= st.executeQuery(&quot;select * from product&quot;);
	*/
	
	
	/********************   不同之处    ***********************/
	PreparedStatement st= conn.prepareStatement(&quot;select * from product&quot;);
	// 预处理SQL语句
	
	ResultSet rs= st.executeQuery();
	//不带参数
	/***********************************************************/
	
	while(rs.next())
	{
		out.print(rs.getString(&quot;pid&quot;)+&quot;,&quot;+rs.getString(&quot;name&quot;)+&quot;&lt;br&gt;&quot;);
	}
	
	rs.close();
	st.close();
	conn.close();
%&gt;
&lt;/body&gt;
&lt;/html&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br></div></div><h3 id="简易示例-带参数添加数据。"><a href="#简易示例-带参数添加数据。" class="header-anchor">#</a> 简易示例：带参数添加数据。</h3> <p><strong>在上例的注释&quot;不同之处&quot;位置上一行，插入添加数据的代码</strong></p> <div class="language-JAVA line-numbers-mode"><pre class="language-java"><code>	<span class="token operator">/</span>此次位置的连接数据库的代码与上例同<span class="token operator">*</span><span class="token operator">/</span>
	
	
	<span class="token comment">/*   使用预处理语句，插入数据  */</span>
	<span class="token class-name">PreparedStatement</span> st<span class="token punctuation">;</span>
	st<span class="token operator">=</span>conn<span class="token punctuation">.</span><span class="token function">prepareStatement</span><span class="token punctuation">(</span><span class="token string">&quot;insert into product(number,name,madeTime,price) values(?,?,?,?)&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>  <span class="token comment">//带命令参数的SQL语句</span>
	
	<span class="token comment">//记录1</span>
	st<span class="token punctuation">.</span><span class="token function">setString</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">&quot;型号1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//参数位置(从1开始)，参数值</span>
	st<span class="token punctuation">.</span><span class="token function">setString</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">&quot;品名1&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	st<span class="token punctuation">.</span><span class="token function">setFloat</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">12.4f</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	java<span class="token punctuation">.</span>util<span class="token punctuation">.</span><span class="token class-name">Date</span> uDate<span class="token operator">=</span><span class="token keyword">new</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span><span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//当前日期</span>
	<span class="token class-name">Date</span> sqlDate<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span>uDate<span class="token punctuation">.</span><span class="token function">getTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	st<span class="token punctuation">.</span><span class="token function">setDate</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> sqlDate<span class="token punctuation">)</span><span class="token punctuation">;</span>
	st<span class="token punctuation">.</span><span class="token function">executeUpdate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	st<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">//记录2</span>
st<span class="token operator">=</span>conn<span class="token punctuation">.</span><span class="token function">prepareStatement</span><span class="token punctuation">(</span><span class="token string">&quot;insert into product(number,name,madeTime,price) values(?,?,?,?)&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>   
	
	st<span class="token punctuation">.</span><span class="token function">setString</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">,</span> <span class="token string">&quot;型号2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	st<span class="token punctuation">.</span><span class="token function">setString</span><span class="token punctuation">(</span><span class="token number">2</span><span class="token punctuation">,</span><span class="token string">&quot;品名2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	st<span class="token punctuation">.</span><span class="token function">setFloat</span><span class="token punctuation">(</span><span class="token number">4</span><span class="token punctuation">,</span> <span class="token number">22.4f</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	
	uDate<span class="token operator">=</span><span class="token keyword">new</span> java<span class="token punctuation">.</span>util<span class="token punctuation">.</span><span class="token class-name">Date</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//当前日期</span>
	sqlDate<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">Date</span><span class="token punctuation">(</span>uDate<span class="token punctuation">.</span><span class="token function">getTime</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	st<span class="token punctuation">.</span><span class="token function">setDate</span><span class="token punctuation">(</span><span class="token number">3</span><span class="token punctuation">,</span> sqlDate<span class="token punctuation">)</span><span class="token punctuation">;</span>
	st<span class="token punctuation">.</span><span class="token function">executeUpdate</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
st<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	 
	
	<span class="token comment">//显示插入的数据  </span>
	<span class="token comment">/********************   不同之处(上例的位置，插入了上述代码)***********************/</span>
	st<span class="token operator">=</span> conn<span class="token punctuation">.</span><span class="token function">prepareStatement</span><span class="token punctuation">(</span><span class="token string">&quot;select * from product&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">// 预处理SQL语句	</span>
	<span class="token class-name">ResultSet</span> rs<span class="token operator">=</span> st<span class="token punctuation">.</span><span class="token function">executeQuery</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>	<span class="token comment">//默认参数</span>

	<span class="token comment">/***********************************************************/</span>
	
	<span class="token keyword">while</span><span class="token punctuation">(</span>rs<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span>
	<span class="token punctuation">{</span>
		out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>rs<span class="token punctuation">.</span><span class="token function">getString</span><span class="token punctuation">(</span><span class="token string">&quot;pid&quot;</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">&quot;,&quot;</span><span class="token operator">+</span>rs<span class="token punctuation">.</span><span class="token function">getString</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token operator">+</span><span class="token string">&quot;&lt;br&gt;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	
	<span class="token comment">/* ***********    省略的关闭代码...     *********/</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br></div></div><h3 id="sql注入示例-登录"><a href="#sql注入示例-登录" class="header-anchor">#</a> SQL注入示例：登录</h3> <div class="language-JAVA line-numbers-mode"><pre class="language-java"><code>	<span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">doGet</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
		<span class="token comment">// TODO Auto-generated method stub</span>
		response<span class="token punctuation">.</span><span class="token function">setContentType</span><span class="token punctuation">(</span><span class="token string">&quot;text/html;charset=utf-8&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        
		<span class="token class-name">String</span> user<span class="token operator">=</span>request<span class="token punctuation">.</span><span class="token function">getParameter</span><span class="token punctuation">(</span><span class="token string">&quot;user&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">String</span> pwd<span class="token operator">=</span>request<span class="token punctuation">.</span><span class="token function">getParameter</span><span class="token punctuation">(</span><span class="token string">&quot;pwd&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        
		<span class="token class-name">PrintWriter</span> out<span class="token operator">=</span>response<span class="token punctuation">.</span><span class="token function">getWriter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">//.append(&quot;Served at: &quot;).append(request.getContextPath());</span>
		<span class="token class-name">String</span> connstring<span class="token operator">=</span>&quot;jdbc<span class="token operator">:</span>mysql<span class="token operator">:</span><span class="token operator">/</span><span class="token operator">/</span>localhost<span class="token operator">:</span><span class="token number">3306</span><span class="token operator">/</span>del<span class="token operator">?</span>
            user<span class="token operator">=</span>yan1234<span class="token operator">&amp;</span>password<span class="token operator">=</span><span class="token number">1234</span><span class="token operator">&amp;</span>characterEncoding<span class="token operator">=</span>utf<span class="token operator">-</span><span class="token number">8</span><span class="token operator">&amp;</span>
            serverTimezone<span class="token operator">=</span><span class="token class-name">Asia</span><span class="token operator">/</span><span class="token class-name">Shanghai</span>&quot;<span class="token punctuation">;</span>
            
            
		<span class="token keyword">try</span> <span class="token punctuation">{</span>
			<span class="token class-name">Connection</span> conn<span class="token operator">=</span><span class="token class-name">DriverManager</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span>connstring<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token class-name">Statement</span> st<span class="token operator">=</span>conn<span class="token punctuation">.</span><span class="token function">createStatement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token class-name">String</span> sql<span class="token operator">=</span><span class="token string">&quot;select count(*) as ts from user where &quot;</span><span class="token punctuation">;</span>
			sql<span class="token operator">+=</span><span class="token string">&quot; username='&quot;</span><span class="token operator">+</span>user<span class="token operator">+</span><span class="token string">&quot;' and pwd='&quot;</span><span class="token operator">+</span>pwd<span class="token operator">+</span><span class="token string">&quot;'&quot;</span><span class="token punctuation">;</span>
			
			out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>sql<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token comment">//select count(*) as ts from user where username='123' and pwd='' </span>
            <span class="token comment">//OR '1=1'</span>
			<span class="token class-name">ResultSet</span> rs<span class="token operator">=</span>st<span class="token punctuation">.</span><span class="token function">executeQuery</span><span class="token punctuation">(</span>sql<span class="token punctuation">)</span><span class="token punctuation">;</span>
			rs<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			
			<span class="token keyword">int</span> num<span class="token operator">=</span>rs<span class="token punctuation">.</span><span class="token function">getInt</span><span class="token punctuation">(</span><span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>num<span class="token operator">&gt;</span><span class="token number">0</span><span class="token punctuation">)</span> out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;登录成功！&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">else</span> out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;登录失败&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			
			
			st<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			rs<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			conn<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			
		<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">SQLException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token comment">// TODO Auto-generated catch block</span>
			e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
		
	
	<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br></div></div><h3 id="_12-事务"><a href="#_12-事务" class="header-anchor">#</a> 12. 事务</h3> <p>​	简单而言，执行一组SQL语句，如果一组SQL语句中，有任何一条没有执行成功，则取消该语句之前的所有语句的操作。</p> <p>​	执行SQL语句之前，需要取消自动提交模式：conn.setAutoCommit(false);直到执行：conn.commit();</p> <p>​	如果执行异常，则执行conn.rollback()方法，取消已执行的sql语句。</p> <h3 id="应用场景示例-模拟转账功能。"><a href="#应用场景示例-模拟转账功能。" class="header-anchor">#</a> 应用场景示例：模拟转账功能。</h3> <p>​    用户A: userid：A001； money:100 ;</p> <p>​	用户B：userid：A002；money:200;</p> <p>​	用户A=&gt;用户B 转账 50  ，如果转账失败，取消对用户A的扣款操作。</p> <div class="language-JAVA line-numbers-mode"><pre class="language-java"><code><span class="token operator">&lt;</span><span class="token operator">%</span>@ page language<span class="token operator">=</span><span class="token string">&quot;java&quot;</span> contentType<span class="token operator">=</span><span class="token string">&quot;text/html; charset=UTF-8&quot;</span>
	pageEncoding<span class="token operator">=</span><span class="token string">&quot;UTF-8&quot;</span><span class="token operator">%</span><span class="token operator">&gt;</span>

<span class="token operator">&lt;</span><span class="token operator">%</span>@ page <span class="token keyword">import</span><span class="token operator">=</span><span class="token string">&quot;java.sql.*&quot;</span><span class="token operator">%</span><span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">!</span>DOCTYPE html<span class="token operator">&gt;</span>
<span class="token generics"><span class="token punctuation">&lt;</span>html<span class="token punctuation">&gt;</span></span>
<span class="token generics"><span class="token punctuation">&lt;</span>head<span class="token punctuation">&gt;</span></span>
<span class="token operator">&lt;</span>meta charset<span class="token operator">=</span><span class="token string">&quot;UTF-8&quot;</span><span class="token operator">&gt;</span>
<span class="token generics"><span class="token punctuation">&lt;</span>title<span class="token punctuation">&gt;</span></span><span class="token class-name">Insert</span> title here<span class="token operator">&lt;</span><span class="token operator">/</span>title<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>head<span class="token operator">&gt;</span>
<span class="token generics"><span class="token punctuation">&lt;</span>body<span class="token punctuation">&gt;</span></span>
	<span class="token operator">&lt;</span><span class="token operator">%</span>
		<span class="token comment">//Class.forName(&quot;com.mysql.cj.jdbc.Driver&quot;);</span>
		<span class="token class-name">String</span> url <span class="token operator">=</span> <span class="token string">&quot;jdbc:mysql://192.168.100.13:3306/warehourse?user=root&amp;password=123&amp;characterEncoding=utf-8&amp;serverTimezone=UTC&quot;</span><span class="token punctuation">;</span>

		<span class="token class-name">Connection</span> conn <span class="token operator">=</span> <span class="token class-name">DriverManager</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span>url<span class="token punctuation">)</span><span class="token punctuation">;</span>

		<span class="token keyword">try</span> <span class="token punctuation">{</span>
			conn<span class="token punctuation">.</span><span class="token function">setAutoCommit</span><span class="token punctuation">(</span><span class="token boolean">false</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

			<span class="token class-name">Statement</span> st <span class="token operator">=</span> conn<span class="token punctuation">.</span><span class="token function">createStatement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

			st<span class="token punctuation">.</span><span class="token function">executeUpdate</span><span class="token punctuation">(</span><span class="token string">&quot;update acount set money=money-50 where userid='A002'&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			st<span class="token punctuation">.</span><span class="token function">executeUpdate</span><span class="token punctuation">(</span><span class="token string">&quot;update acount set money=money+50 where userid='A001'&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token comment">//st.executeUpdate(&quot;&quot;);  模拟异常</span>
			<span class="token comment">//int x=10/0; // 模拟异常</span>
			conn<span class="token punctuation">.</span><span class="token function">commit</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

		<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">Exception</span> ex<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			conn<span class="token punctuation">.</span><span class="token function">rollback</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span>ex<span class="token punctuation">.</span><span class="token function">getMessage</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
	<span class="token operator">%</span><span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>body<span class="token operator">&gt;</span>
<span class="token operator">&lt;</span><span class="token operator">/</span>html<span class="token operator">&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br></div></div><h3 id="数据联动查询"><a href="#数据联动查询" class="header-anchor">#</a> 数据联动查询</h3> <p>bean:DataBean.java</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code><span class="token keyword">package</span> <span class="token namespace">myservlet</span><span class="token punctuation">;</span>

<span class="token keyword">public</span> <span class="token keyword">class</span> <span class="token class-name">DataBean</span> <span class="token punctuation">{</span>
	<span class="token class-name">String</span> code<span class="token punctuation">;</span>
	<span class="token class-name">String</span> name<span class="token punctuation">;</span>
	
	<span class="token comment">//</span>
	<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getCode</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> code<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setCode</span><span class="token punctuation">(</span><span class="token class-name">String</span> code<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>code <span class="token operator">=</span> code<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">public</span> <span class="token class-name">String</span> <span class="token function">getName</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">return</span> name<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token keyword">public</span> <span class="token keyword">void</span> <span class="token function">setName</span><span class="token punctuation">(</span><span class="token class-name">String</span> name<span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">this</span><span class="token punctuation">.</span>name <span class="token operator">=</span> name<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	 
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br></div></div><p>servelt:FindDataServlet.java</p> <div class="language-java line-numbers-mode"><pre class="language-java"><code>	<span class="token keyword">protected</span> <span class="token keyword">void</span> <span class="token function">doGet</span><span class="token punctuation">(</span><span class="token class-name">HttpServletRequest</span> request<span class="token punctuation">,</span> <span class="token class-name">HttpServletResponse</span> response<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">ServletException</span><span class="token punctuation">,</span> <span class="token class-name">IOException</span> <span class="token punctuation">{</span>
		<span class="token comment">// TODO Auto-generated method stub</span>
		response<span class="token punctuation">.</span><span class="token function">setContentType</span><span class="token punctuation">(</span><span class="token string">&quot;text/html;charset=utf-8&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">PrintWriter</span> out<span class="token operator">=</span>response<span class="token punctuation">.</span><span class="token function">getWriter</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span><span class="token comment">//.append(&quot;Served at: &quot;).append(request.getContextPath());</span>
		<span class="token class-name">String</span> connstr<span class="token operator">=</span><span class="token string">&quot;jdbc:mysql://localhost:3306/del?user=yan1234&amp;password=1234&amp;characterEncoding=utf-8&amp;serverTimezone=Asia/Shanghai&quot;</span><span class="token punctuation">;</span>
		
		<span class="token class-name">String</span> save<span class="token operator">=</span>request<span class="token punctuation">.</span><span class="token function">getParameter</span><span class="token punctuation">(</span><span class="token string">&quot;isSave&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">if</span><span class="token punctuation">(</span>save<span class="token operator">!=</span><span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
			out<span class="token punctuation">.</span><span class="token function">print</span><span class="token punctuation">(</span><span class="token string">&quot;执行注册数据保存操作...&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token comment">//读取表单数据，开始保存</span>
			<span class="token keyword">return</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span>
		
		<span class="token comment">//是否提交保存</span>
		<span class="token keyword">try</span> <span class="token punctuation">{</span>
			<span class="token class-name">Connection</span> conn<span class="token operator">=</span><span class="token class-name">DriverManager</span><span class="token punctuation">.</span><span class="token function">getConnection</span><span class="token punctuation">(</span>connstr<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token class-name">Statement</span> st<span class="token operator">=</span>conn<span class="token punctuation">.</span><span class="token function">createStatement</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			
			<span class="token comment">//判断session是否存在prov数据，不存在表示首次打开</span>
			<span class="token class-name">HttpSession</span> session<span class="token operator">=</span>request<span class="token punctuation">.</span><span class="token function">getSession</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			 
			<span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">DataBean</span><span class="token punctuation">&gt;</span></span> provList<span class="token operator">=</span><span class="token punctuation">(</span><span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">DataBean</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">)</span>session<span class="token punctuation">.</span><span class="token function">getAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;provList&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token keyword">if</span><span class="token punctuation">(</span>provList<span class="token operator">==</span><span class="token keyword">null</span><span class="token punctuation">)</span> <span class="token punctuation">{</span><span class="token comment">//首次打开</span>
				provList<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">DataBean</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token comment">//读取省份数据，在根据省份第1项，读取城市数据</span>
				<span class="token class-name">String</span> sql<span class="token operator">=</span><span class="token string">&quot;select province_code as code,province_name as name from bs_province&quot;</span><span class="token punctuation">;</span>
				<span class="token class-name">ResultSet</span> rs<span class="token operator">=</span>st<span class="token punctuation">.</span><span class="token function">executeQuery</span><span class="token punctuation">(</span>sql<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token keyword">while</span><span class="token punctuation">(</span>rs<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
					<span class="token class-name">DataBean</span> bean<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">DataBean</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
					bean<span class="token punctuation">.</span>code<span class="token operator">=</span>rs<span class="token punctuation">.</span><span class="token function">getString</span><span class="token punctuation">(</span><span class="token string">&quot;code&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
					bean<span class="token punctuation">.</span>name<span class="token operator">=</span>rs<span class="token punctuation">.</span><span class="token function">getString</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
					provList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>bean<span class="token punctuation">)</span><span class="token punctuation">;</span> 
				<span class="token punctuation">}</span>
				rs<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				session<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;provList&quot;</span><span class="token punctuation">,</span> provList<span class="token punctuation">)</span><span class="token punctuation">;</span>
				
				<span class="token comment">//城市</span>
				 
				<span class="token class-name">String</span> code1<span class="token operator">=</span>provList<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span>code<span class="token punctuation">;</span><span class="token comment">//取出第一个元素，根据第1个元素获取城市数据</span>
				code1<span class="token operator">=</span><span class="token class-name">FindCity</span><span class="token punctuation">(</span>code1<span class="token punctuation">,</span>st<span class="token punctuation">,</span>session<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token class-name">FindArea</span><span class="token punctuation">(</span>code1<span class="token punctuation">,</span>st<span class="token punctuation">,</span>session<span class="token punctuation">)</span><span class="token punctuation">;</span>
			<span class="token punctuation">}</span>
			<span class="token keyword">else</span><span class="token comment">//数据提交</span>
			<span class="token punctuation">{</span>
				<span class="token comment">//已经存在数据，那么是表单提交触发的</span>
				<span class="token comment">//判断是省份，还是城市触发，再读取数据</span>
				<span class="token class-name">String</span> hidden_prov_code<span class="token operator">=</span>request<span class="token punctuation">.</span><span class="token function">getParameter</span><span class="token punctuation">(</span><span class="token string">&quot;hidden_prov_code&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token class-name">String</span> hidden_city_code<span class="token operator">=</span>request<span class="token punctuation">.</span><span class="token function">getParameter</span><span class="token punctuation">(</span><span class="token string">&quot;hidden_city_code&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
				
				<span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>hidden_prov_code<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">&quot;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
				<span class="token comment">//城市</span>
					<span class="token class-name">String</span> cityCode<span class="token operator">=</span><span class="token class-name">FindCity</span><span class="token punctuation">(</span>hidden_prov_code<span class="token punctuation">,</span>st<span class="token punctuation">,</span>session<span class="token punctuation">)</span><span class="token punctuation">;</span>
					<span class="token class-name">FindArea</span><span class="token punctuation">(</span>cityCode<span class="token punctuation">,</span>st<span class="token punctuation">,</span>session<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token punctuation">}</span>
				<span class="token keyword">else</span> <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token operator">!</span>hidden_city_code<span class="token punctuation">.</span><span class="token function">equals</span><span class="token punctuation">(</span><span class="token string">&quot;&quot;</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
					<span class="token class-name">FindArea</span><span class="token punctuation">(</span>hidden_city_code<span class="token punctuation">,</span>st<span class="token punctuation">,</span>session<span class="token punctuation">)</span><span class="token punctuation">;</span>
				<span class="token punctuation">}</span>
				
			<span class="token punctuation">}</span>
			
			
			st<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			conn<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			
			request<span class="token punctuation">.</span><span class="token function">getRequestDispatcher</span><span class="token punctuation">(</span><span class="token string">&quot;showData.jsp&quot;</span><span class="token punctuation">)</span><span class="token punctuation">.</span><span class="token function">forward</span><span class="token punctuation">(</span>request<span class="token punctuation">,</span> response<span class="token punctuation">)</span><span class="token punctuation">;</span>
			
		<span class="token punctuation">}</span> <span class="token keyword">catch</span> <span class="token punctuation">(</span><span class="token class-name">SQLException</span> e<span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token comment">// TODO Auto-generated catch block</span>
			e<span class="token punctuation">.</span><span class="token function">printStackTrace</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token punctuation">}</span> 
	<span class="token punctuation">}</span>

	<span class="token class-name">String</span> <span class="token class-name">FindCity</span><span class="token punctuation">(</span><span class="token class-name">String</span> provCode<span class="token punctuation">,</span><span class="token class-name">Statement</span> st<span class="token punctuation">,</span> <span class="token class-name">HttpSession</span> session<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">SQLException</span> <span class="token punctuation">{</span>
 	<span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">DataBean</span><span class="token punctuation">&gt;</span></span> cityList<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">DataBean</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token comment">//String code1=provList.get(0).code;//取出第一个元素，根据第1个元素获取城市数据</span>
		<span class="token class-name">String</span> sql<span class="token operator">=</span><span class="token string">&quot;select city_code as code,city_name as name from bs_city where province_code='&quot;</span><span class="token operator">+</span>provCode<span class="token operator">+</span><span class="token string">&quot;'&quot;</span><span class="token punctuation">;</span>
		<span class="token class-name">ResultSet</span> rs<span class="token operator">=</span>st<span class="token punctuation">.</span><span class="token function">executeQuery</span><span class="token punctuation">(</span>sql<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">while</span><span class="token punctuation">(</span>rs<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token class-name">DataBean</span> bean<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">DataBean</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			bean<span class="token punctuation">.</span>code<span class="token operator">=</span>rs<span class="token punctuation">.</span><span class="token function">getString</span><span class="token punctuation">(</span><span class="token string">&quot;code&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			bean<span class="token punctuation">.</span>name<span class="token operator">=</span>rs<span class="token punctuation">.</span><span class="token function">getString</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			cityList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>bean<span class="token punctuation">)</span><span class="token punctuation">;</span> 
		<span class="token punctuation">}</span>
		rs<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">String</span> cityCode<span class="token operator">=</span>cityList<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span>code<span class="token punctuation">;</span>
		session<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;cityList&quot;</span><span class="token punctuation">,</span> cityList<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">return</span> cityCode<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
	<span class="token class-name">String</span> <span class="token class-name">FindArea</span><span class="token punctuation">(</span><span class="token class-name">String</span> cityCode<span class="token punctuation">,</span><span class="token class-name">Statement</span> st<span class="token punctuation">,</span> <span class="token class-name">HttpSession</span> session<span class="token punctuation">)</span> <span class="token keyword">throws</span> <span class="token class-name">SQLException</span> <span class="token punctuation">{</span>
		<span class="token class-name">List</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">DataBean</span><span class="token punctuation">&gt;</span></span> areaList<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">ArrayList</span><span class="token generics"><span class="token punctuation">&lt;</span><span class="token class-name">DataBean</span><span class="token punctuation">&gt;</span></span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token comment">//String code1=provList.get(0).code;//取出第一个元素，根据第1个元素获取城市数据</span>
		<span class="token class-name">String</span> sql<span class="token operator">=</span><span class="token string">&quot;select area_code as code,area_name as name from bs_area where city_code='&quot;</span><span class="token operator">+</span>cityCode<span class="token operator">+</span><span class="token string">&quot;'&quot;</span><span class="token punctuation">;</span>
		<span class="token class-name">ResultSet</span> rs<span class="token operator">=</span>st<span class="token punctuation">.</span><span class="token function">executeQuery</span><span class="token punctuation">(</span>sql<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">while</span><span class="token punctuation">(</span>rs<span class="token punctuation">.</span><span class="token function">next</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token class-name">DataBean</span> bean<span class="token operator">=</span><span class="token keyword">new</span> <span class="token class-name">DataBean</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			bean<span class="token punctuation">.</span>code<span class="token operator">=</span>rs<span class="token punctuation">.</span><span class="token function">getString</span><span class="token punctuation">(</span><span class="token string">&quot;code&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			bean<span class="token punctuation">.</span>name<span class="token operator">=</span>rs<span class="token punctuation">.</span><span class="token function">getString</span><span class="token punctuation">(</span><span class="token string">&quot;name&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
			areaList<span class="token punctuation">.</span><span class="token function">add</span><span class="token punctuation">(</span>bean<span class="token punctuation">)</span><span class="token punctuation">;</span> 
		<span class="token punctuation">}</span>
		rs<span class="token punctuation">.</span><span class="token function">close</span><span class="token punctuation">(</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token class-name">String</span> areaCode<span class="token operator">=</span>areaList<span class="token punctuation">.</span><span class="token function">get</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">.</span>code<span class="token punctuation">;</span>
		session<span class="token punctuation">.</span><span class="token function">setAttribute</span><span class="token punctuation">(</span><span class="token string">&quot;areaList&quot;</span><span class="token punctuation">,</span> areaList<span class="token punctuation">)</span><span class="token punctuation">;</span>
		<span class="token keyword">return</span> areaCode<span class="token punctuation">;</span>
	<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br><span class="line-number">84</span><br><span class="line-number">85</span><br><span class="line-number">86</span><br><span class="line-number">87</span><br><span class="line-number">88</span><br><span class="line-number">89</span><br><span class="line-number">90</span><br><span class="line-number">91</span><br><span class="line-number">92</span><br><span class="line-number">93</span><br><span class="line-number">94</span><br><span class="line-number">95</span><br><span class="line-number">96</span><br><span class="line-number">97</span><br><span class="line-number">98</span><br><span class="line-number">99</span><br><span class="line-number">100</span><br><span class="line-number">101</span><br><span class="line-number">102</span><br><span class="line-number">103</span><br><span class="line-number">104</span><br></div></div><p>jsp:showData.jsp</p> <div class="language-JSP line-numbers-mode"><pre class="language-text"><code>&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;
    pageEncoding=&quot;UTF-8&quot; import=&quot;java.util.*,myservlet.*&quot;%&gt;
&lt;!DOCTYPE html&gt;
&lt;html&gt;
&lt;head&gt;
&lt;meta charset=&quot;UTF-8&quot;&gt;
&lt;title&gt;Insert title here&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;!--  
1.显示数据内容Bean：代码，名称
2.数据：读取到List&lt;Bean&gt;
3.初始数据没有，读取，保存
4.默认选择第1项，继续选择，保存
5.转发
6.触发第1个项，手动提交，读取改变的第2项数据；保持第1项内容，再次转发
7.读取保持的第1项，默认选择第2项
8.触发第2项，判断第2项，选择第3项内容...
判断依据和方法：触发了哪一项，可以使用隐藏文本框；获取这项并来回转发
--&gt;
&lt;%
List&lt;DataBean&gt; provList=(List&lt;DataBean&gt;)session.getAttribute(&quot;provList&quot;);
List&lt;DataBean&gt; cityList=(List&lt;DataBean&gt;)session.getAttribute(&quot;cityList&quot;);
List&lt;DataBean&gt; areaList=(List&lt;DataBean&gt;)session.getAttribute(&quot;areaList&quot;);
if(provList==null) {
	response.sendRedirect(&quot;FindDataServlet&quot;);
	return;
}


String hidden_prov_code=request.getParameter(&quot;prov&quot;);
String hidden_city_code=request.getParameter(&quot;city&quot;);

%&gt;
&lt;form action=&quot;FindDataServlet&quot; method=&quot;post&quot; id=&quot;form1&quot; onsubmit=&quot;submit1(event)&quot;&gt;
		&lt;!-- province --&gt;
		&lt;select name=&quot;prov&quot; id=&quot;prov&quot;  onchange=&quot;provChange(event)&quot;&gt;
				&lt;% for(DataBean bean:provList){ %&gt;
						&lt;option 
						value=&quot;&lt;%=bean.getCode() %&gt;&quot; 
						&lt;%= (hidden_prov_code!=null &amp;&amp; hidden_prov_code.equals(bean.getCode()))?&quot;selected&quot;:&quot;&quot; %&gt;
						&gt;
						
						&lt;%= bean.getName() %&gt;
						
						&lt;/option&gt;
				&lt;%} %&gt;
		&lt;/select&gt;
		&lt;!-- city --&gt;
		&lt;select name=&quot;city&quot;  id=&quot;city&quot; onchange=&quot;cityChange(event)&quot;&gt;
				&lt;% for(DataBean bean:cityList){ %&gt;
						&lt;option value=&quot;&lt;%=bean.getCode() %&gt;&quot; 
						&lt;%= hidden_city_code!=null&amp;&amp; hidden_city_code.equals(bean.getCode())?&quot;selected&quot;:&quot;&quot; %&gt;
						&gt;&lt;%= bean.getName() %&gt;&lt;/option&gt;
				&lt;%} %&gt;
		&lt;/select&gt;
		
		&lt;!-- area --&gt;		
		&lt;select name=&quot;area&quot;  id=&quot;area&quot;&gt;
				&lt;% for(DataBean bean:areaList){ %&gt;
						&lt;option value=&quot;&lt;%=bean.getCode() %&gt;&quot; &gt;&lt;%= bean.getName() %&gt;&lt;/option&gt;
				&lt;%} %&gt;
		&lt;/select&gt;
		
		&lt;br/&gt;
		&lt;!-- 标识 --&gt;
		&lt;input type=&quot;hidden&quot; name=&quot;hidden_prov_code&quot; id=&quot;hidden_prov_code&quot;/&gt;
		&lt;input type=&quot;hidden&quot; name=&quot;hidden_city_code&quot; id=&quot;hidden_city_code&quot;/&gt;
		
		
		&lt;!-- 其他表单元素 --&gt;
		&lt;br/&gt;
		x1：&lt;input name=&quot;x1&quot; value='&lt;%=request.getParameter(&quot;x1&quot;) %&gt;'/&gt;
		&lt;br/&gt;
		&lt;input type=&quot;checkbox&quot; name=&quot;isSave&quot; id=&quot;issave&quot; value=&quot;save&quot;&gt;我已经阅读了注册协议
		&lt;button  &gt;保存&lt;/button&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;

&lt;script&gt;
	function provChange(ev){
		hidden_prov_code.value= ev.target.value 
		issave.checked=false
		form1.submit() 
	}
	
	function cityChange(ev){
		hidden_city_code.value= ev.target.value 
		issave.checked=false
		form1.submit() 
	}
	
	function submit1(ev){
		ev.preventDefault()
		if(issave.checked==false){
			alert(&quot;请勾选协议！&quot;)
		}
		else{
			form1.submit()
			}
	}

&lt;/script&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br><span class="line-number">84</span><br><span class="line-number">85</span><br><span class="line-number">86</span><br><span class="line-number">87</span><br><span class="line-number">88</span><br><span class="line-number">89</span><br><span class="line-number">90</span><br><span class="line-number">91</span><br><span class="line-number">92</span><br><span class="line-number">93</span><br><span class="line-number">94</span><br><span class="line-number">95</span><br><span class="line-number">96</span><br><span class="line-number">97</span><br><span class="line-number">98</span><br><span class="line-number">99</span><br><span class="line-number">100</span><br><span class="line-number">101</span><br><span class="line-number">102</span><br><span class="line-number">103</span><br><span class="line-number">104</span><br></div></div></div> <footer class="page-edit"><!----> <!----></footer> <div class="page-nav"><p class="inner"><span class="prev">
      ←
      <a href="/blog/views/web/v3.html" class="prev">
        Web 存储
      </a></span> <span class="next"><a href="/blog/views/software/01.html">
        软件工程实践复习
      </a>
      →
    </span></p></div> </main></div><div class="global-ui"></div></div>
    <script src="/blog/assets/js/app.ce46b86f.js" defer></script><script src="/blog/assets/js/2.262e1e49.js" defer></script><script src="/blog/assets/js/8.db580af8.js" defer></script>
  </body>
</html>
